import Error404 from '@/pages/error/Error404';

import App from '@/App';
import { SESSION_LOGIN_INFO } from '@/constants';
import { ErrorBoundary } from '@/pages/error/ErrorBoundary';
import Login from '@/pages/user/Login';
import type { IRouteItem } from '@/routes/RouteType';
import { Navigate } from 'react-router-dom';
import { homeRoutes, mainRoutes } from './modules/menus';

/**
 * 使用glob完成自动化导入，自动生成约定式路由routes
 * Index.tsx 路由入口文件
 * page.ts 路由属性配置
 */

let routes: IRouteItem[] = [
  { path: '/login', element: <Login /> },
  {
    path: '/',
    label: '首页',
    element: <App />,
    children: [
      {
        path: '/',
        label: '首页',
        element: <Navigate to={'/home'} />,
      },
      ...homeRoutes,
      ...mainRoutes,
      {
        path: '/*',
        label: 'Error404',
        element: <Error404 />,
        errorElement: <ErrorBoundary />,
      },
    ],
  },
  {
    path: '*',
    label: 'Error404',
    element: <Error404 />,
  },
];

// 路由守卫
export function PrivateRoute(props: any) {
  // 判断localStorage是否有登录用户信息，如果没有则跳转登录页
  return window.sessionStorage.getItem(SESSION_LOGIN_INFO) ? (
    props.children
  ) : (
    <Navigate to="/login" />
  );
}

export default routes;
